<?php

/**
 * Generovani playlistu na zaklade XSPF
 * @see http://xspf.org/
 */

class Playlist_Xml
{

    /**
     *
     * @var DomDocument
     */
    private $_dom;

    /**
     *
     * @param Playlist $playlist 
     */

    public function __construct(Playlist $playlist)
    {

        $dom = new DOMDocument('1.0', 'UTF-8'); //nastaveni verze a formatu
        $dom->formatOutput = "true"; //formatovani souboru

        /*
         * blok pro zaobalujici element playlist
         */
        $x_playlist = $dom->createElement('playlist');
        $x_playlist_version = $dom->createAttribute('version');
        $x_playlist_version_text = $dom->createTextNode('1');
        $x_playlist_version->appendChild($x_playlist_version_text);
        $x_playlist_xmlns = $dom->createAttribute('xmlns');
        $x_playlist_xmlns_text = $dom->createTextNode('http://xspf.org/ns/0/');
        $x_playlist_xmlns->appendChild($x_playlist_xmlns_text);
        $x_playlist->appendChild($x_playlist_version);
        $x_playlist->appendChild($x_playlist_xmlns);

        $x_trackList = $dom->createElement('trackList'); //element trackList
        $x_playlist->appendChild($x_trackList);
        $dom->appendChild($x_playlist); //pridani elementu playlist

        $tracks = new DbTracks();

        foreach ($playlist->getTracks() as $track)
        {
            if (is_numeric($track))
            {
                $track = $tracks->find($track)->current();
            }

            //element track
            $x_track = $dom->createElement('track');

            //element location
            $x_location = $dom->createElement('location');
            $x_location_text = $dom->createTextNode($track->getUrl());
            $x_location->appendChild($x_location_text);

            //element creator
            $x_creator = $dom->createElement('creator');
            $x_creator_text = $dom->createTextNode($track->artist_name);
            $x_creator->appendChild($x_creator_text);

            //element album
            $x_album = $dom->createElement('album');
            $x_album_text = $dom->createTextNode($track->getAlbum());
            $x_album->appendChild($x_album_text);

            //element title
            $x_title = $dom->createElement('title');
            $x_title_text = $dom->createTextNode($track->getSong());
            $x_title->appendChild($x_title_text);

            $x_track->appendChild($x_location); //pridani lokace
            $x_track->appendChild($x_creator); //pridani umelce
            $x_track->appendChild($x_album); //pridani umelce
            $x_track->appendChild($x_title); //pridani nazvu pisnicky

            $x_trackList->appendChild($x_track);
        }

        $dom->normalizeDocument();

        $this->_dom = $dom;
    }

    /**
     *
     * @return string
     */
    public function getXml()
    {
        return $this->_dom->saveXML();
    }

    /**
     *
     * @return DomDocument
     */

    public function getDom()
    {
        return $this->_dom;
    }

}